<?php

namespace App\Command\ResumeParsing\Service\Dict;

use App\Service\DictConstant;

class DictHelper {
    private $dictSource;
    private $dicts;

    public function __construct() {
        $this->dictSource = new \App\Service\Dict();
        $this->dictSource->setLang(DictConstant::LANG_ZH);
        $this->dicts = [true => [], false => []];
    }

    //地区编码
    public function getAreaDict() {
        $areaArr = include(__DIR__ . '/aeraDict.php');
        return $areaArr;
    }

    //专业
    public function getMajorDict() {
        $majorArr = include(__DIR__ . '/majorDict.php');
        return $majorArr;
    }

    //行业
    public function getIndustryDict() {
        $industryArr = include(__DIR__ . '/industryDict.php');
        return $industryArr;
    }

    //工作类型
    public function getJobtermDict() {
        return $this->getDict(DictConstant::DD_JOB_TERM);
    }

    //求职状态
    public function getCurrentSituationDict() {
        return $this->getDict(DictConstant::DD_CURRENT_SITUATION);
    }

    //到岗时间
    public function getEntryTimeDict() {
        return $this->getDict(DictConstant::DD_NEW_JOB_ENTRY_TIME);
    }

    //性别
    public function getSexDict() {
        return $this->getDict(DictConstant::DD_GENDER);
    }

    //婚姻情况
    public function getMarriedDict() {
        return $this->getDict(DictConstant::DD_MARRIAGE);
    }

    //政治面貌
    public function getPoliticDict() {
        return $this->getDict(DictConstant::DD_POLITICS);
    }

    //学历
    public function getDegreeDict() {
        return $this->getDict(DictConstant::DD_DEGREE);
    }

    protected function initDict($type, $flip = true) {
        $result = array_map(function ($text) {
            return preg_replace('/[^\x{4e00}-\x{9fff}a-zA-Z0-9]+/u', '', strtolower($text));
        }, $this->dictSource->all($type));
        if ($flip) {
            $result = array_flip($result);
            switch ($type) {
                case DictConstant::DD_DEGREE:
                    $result['emba'] = $result['mba'];
                    break;
                case DictConstant::DD_NEW_JOB_ENTRY_TIME:
                    $result['随时'] = $result['即时'];
                    $result['1周内'] = $result['一周以内'];
                    $result['1个月内'] = $result['一个月内'];
                    $result['3个月内'] = $result['三个月内'];
                    break;
                default:
            }
        }
        return $result;
    }

    public function &getDict($type, $flip = true) {
        if (!isset($this->dicts[$flip][$type])) $this->dicts[$flip][$type] = $this->initDict($type, $flip);
        return $this->dicts[$flip][$type];
    }

    public function getValue($type, $text, $flip = true) {
        $dict = &$this->getDict($type, $flip);
        if (is_array($dict) && isset($dict[$text])) return $dict[$text];
        return '';
    }
}